package com.kingdee.eas.xbang.task.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.Vector;

import com.kingdee.bos.ctrl.swing.KDPanel;
import com.kingdee.eas.common.client.LocaleUtil;
import com.kingdee.eas.common.client.SysContext;

public class MyBgPanel extends KDPanel {

	public MyBgPanel(Image bgImg, String msg) {
		this.bgImg = bgImg;
		this.msg = msg;
		setBackground(extMsgBgColor);
		setPreferredSize(new Dimension(429, 165));
		if (msg != null && !msg.equals(""))
			initStringArray();
	}

	protected void initStringArray() {
		FontMetrics fontMetrics = getFontMetrics(getFont());
		StringTokenizer st = new StringTokenizer(msg, "\n");
		String paragraphString = null;
		boolean isChinese = false;
		String language = null;
		try {
			language = LocaleUtil.getOriginalLocale(SysContext.getSysContext().getLocale()).getLanguage();
		} catch (Throwable t) {
			language = Locale.getDefault().getLanguage();
		}
		if (language != null && language.equalsIgnoreCase("zh"))
			isChinese = true;
		vecLines = new Vector(26);
		StringBuffer lineString = null;
		for (; st.hasMoreTokens() && vecLines.size() < 26; vecLines.add(lineString)) {
			paragraphString = st.nextToken().trim();
			System.out.println(paragraphString);
			if (isChinese)
				paragraphString = "\u3000\u3000" + paragraphString;
			else
				paragraphString = "  " + paragraphString;
			lineString = new StringBuffer();
			int lineLength = 0;
			for (int i = 0; i < paragraphString.length() && vecLines.size() < 26; i++) {
				char word = paragraphString.charAt(i);
				lineLength += fontMetrics.charWidth(word);
				if (lineLength < 255) {
					lineString.append(word);
				} else {
					lineString.append(word);
					vecLines.add(lineString);
					lineString = new StringBuffer();
					lineLength = 0;
				}
			}

		}

		String lastLineStr = vecLines.get(vecLines.size() - 1).toString();
		if (lastLineStr.trim().length() == 1 && "!,.?\uFF01\uFF0C\u3002\uFF1F".indexOf(lastLineStr.trim()) != -1) {
			vecLines.remove(vecLines.size() - 1);
			// AdvMsgBox.logger.info("new line just one char, remove it . this char is:" + lastLineStr.trim());
		}
		if (vecLines.size() > 9 && vecLines.size() < 27) {
			int actHeight = vecLines.size() * 15;
			setPreferredSize(new Dimension(429, actHeight));
		} else if (vecLines.size() > 26)
			setPreferredSize(new Dimension(429, 390));
	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(bgImg, 0, 0, getSize().width, 138, this);
		if (vecLines != null) {
			for (int i = 0; i < vecLines.size(); i++)
				g.drawString(((StringBuffer) vecLines.elementAt(i)).toString(), 150, 28 + i * 15);

		}
	}

	protected Image bgImg;
	protected String msg;
	protected Vector vecLines;
	protected static final int lineHeight = 15;
	protected static final int x = 150;
	protected static final int y = 28;
	protected static final int maxWidth = 255;
	protected static final int normalLine = 8;
	protected static final int maxLine = 26;
	protected static final int imageWidth = 429;
	protected static final int imageHeight = 138;
	protected static final Color extMsgBgColor = new Color(242, 242, 242);
}
